﻿using System;
using System.Collections.Generic;
using CrazyTalk.Models;
using System.Globalization;
using CrazyTalk.Models.EnumList;

namespace CrazyTalk.Common
{
    public enum Formater
    {
        /// <summary>
        /// 12:01:01格式字符串
        /// </summary>
        DateTime_HHmmss,
        /// <summary>
        /// 120101格式字符串
        /// </summary>
        DateTime_HHmmss1,
        /// <summary>
        /// 返回2012-05-12格式字符串
        /// </summary>
        DateTime_yyyyMMdd,

        /// <summary>
        /// 返回20120512格式字符串
        /// </summary>
        DateTime_yyyyMMdd1,
        /// <summary>
        /// 返回2012-05-12 12:01:01格式字符串
        /// </summary>
        DateTime_yyyyMMddHHmmss,
        /// <summary>
        /// yyyyMMddHHmmss
        /// </summary>
        DateTime_yyyyMMddHHmmssNohen,
        /// <summary>
        /// 返回20120512120101格式字符串
        /// </summary>
        DateTime_yyyyMMddHHmmss1,
        /// <summary>
        /// 
        /// </summary>
        DateTime_yyyyMMddBRHHmmss,
        /// <summary>
        /// 
        /// </summary>
        ColorRed,
        /// <summary>
        /// 
        /// </summary>
        ColorBlue,
        /// <summary>
        /// 2位小数百分号
        /// </summary>
        P2,
        /// <summary>
        /// 0位小数
        /// </summary>
        N0,
        /// <summary>
        /// 1位小数
        /// </summary>
        N1,
        /// <summary>
        /// 2位小数
        /// </summary>
        N2,
        /// <summary>
        /// 千分位,0为小数
        /// </summary>
        Thous0,
        /// <summary>
        /// 1位小数
        /// </summary>
        Thous1,
        /// <summary>
        /// 2位小数
        /// </summary>
        Thous2
    }
    
    public static class BizExp
    {
        /// <summary>
        /// 根据角色，获取用户上级网吧连锁公司级的ID
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string GetParent(UserInfo user)
        {
            return "";
            /*
            switch ((RoleType)user.RoleId)
            {
                case RoleType.连锁加盟网吧:
                case RoleType.直营店:
                case RoleType.连锁网吧公司财务员:
                case RoleType.连锁网吧公司管理员:
                case RoleType.连锁公司客服:
                    return user.ParentId;
                case RoleType.网吧收银员:
                case RoleType.网吧管理员:
                case RoleType.网吧客服:
                    UserInfo umod = GetModelById(user.ParentId);
                    return (umod != null ? umod.ParentId : "");
                case RoleType.推广员:
                    if (user.ParentId.ToLower() == "root")
                    {
                        return "root";
                    }
                    else
                    {
                        UserInfo umod2 = GetModelById(user.ParentId);
                        return (umod2 != null ? umod2.ParentId : "");
                    }
                default:
                    return "";
            }*/
        }
        public static string GetParent(UserInfo user, out  int roleid)
        {
            roleid = 0;
            return "";
            /*
            switch ((RoleType)user.RoleId)
            {
                case RoleType.网吧收银员:
                case RoleType.网吧管理员:
                case RoleType.网吧客服:
                    UserInfo umod = GetModelById(user.ParentId);
                    roleid = umod.RoleId;
                    return (umod != null ? umod.ParentId : "");
                default:
                    return "";
            }*/
        }

        public static string ToFormater(this object objValue, params Formater[] formaters)
        {
            string value = objValue.ToString();
            foreach (var f in formaters)
            {
                switch (f)
                {
                    case Formater.DateTime_yyyyMMdd1:
                        value = DateTime_yyyyMMdd1(value);
                        break;
                    case Formater.DateTime_yyyyMMdd:
                        value = DateTime_yyyyMMdd(value);
                        break;
                    case Formater.DateTime_HHmmss:
                        value = DateTime_HHmmss(value);
                        break;
                    case Formater.DateTime_HHmmss1:
                        value = DateTime_HHmmss1(value);
                        break;
                    case Formater.DateTime_yyyyMMddHHmmss1:
                        value = DateTime_yyyyMMddHHmmss1(value);
                        break;
                    case Formater.DateTime_yyyyMMddHHmmss:
                        value = DateTime_yyyyMMddHHmmss(value);
                        break;
                    case Formater.DateTime_yyyyMMddBRHHmmss:
                        value = DateTime_yyyyMMddBRHHmmss(value);
                        break;
                    case Formater.DateTime_yyyyMMddHHmmssNohen:
                        value = DateTime_yyyyMMddHHmmssNohen(value);
                        break;
                    case Formater.ColorRed:
                        value = ColorRed(value);
                        break;
                    case Formater.ColorBlue:
                        value = ColorBlue(value);
                        break;
                    case Formater.N0:
                        value = N0(value);
                        break;
                    case Formater.N1:
                        value = N1(value);
                        break;
                    case Formater.N2:
                        value = N2(value);
                        break;
                    case Formater.P2:
                        value = P2(value);
                        break;
                    case Formater.Thous0:
                        value = Thous0(value);
                        break;
                    case Formater.Thous1:
                        value = Thous1(value);
                        break;
                    case Formater.Thous2:
                        value = Thous2(value);
                        break;
                }
            }
            return value;
        }
        #region 字符串扩展方法
        /*
        /// <summary>
        /// 比默认string.Format高效的替代方法
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string FormatWith(this string format, params object[] args)
        {
            if (format == null || args == null)
            {
                throw new ArgumentNullException((format == null) ? "format" : "args");
            }
            else
            {
                var capacity = format.Length + args.Where(a => a != null).Select(p => p.ToString()).Sum(p => p.Length);
                Console.WriteLine(capacity);
                var stringBuilder = new StringBuilder(capacity);
                stringBuilder.AppendFormat(format, args);
                return stringBuilder.ToString();
            }
        }
        public static string FormatWith(this string[] formats, params object[] args)
        {
            if (formats == null || args == null)
            {
                throw new ArgumentNullException((formats == null) ? "format" : "args");
            }
            else
            {
                var capacity = formats.Where(f => f != null).Sum(f => f.Length) +
                    args.Where(a => a != null).Select(p => p.ToString()).Sum(p => p.Length);
                Console.WriteLine(capacity);
                var stringBuilder = new StringBuilder(capacity);
                for (int i = 0; i < formats.Length; i++)
                {
                    stringBuilder.AppendFormat(formats[i], args);
                }
                return stringBuilder.ToString();
            }
        }


        public static List<T> ToList<T>(this string text, char split)
        {
            if (string.IsNullOrEmpty(text))
            {
                return null;
            }
            string[] strArray = text.Trim(new char[] { split }).Split(new char[] { split });
            if (strArray.Length == 0)
            {
                return null;
            }
            List<T> list = new List<T>();
            try
            {
                foreach (string str in strArray)
                {
                    T item = (T)Convert.ChangeType(str, typeof(T));
                    list.Add(item);
                }
            }
            catch
            {
                return null;
            }
            return list;
        }*/
        #endregion
        public static string ToStringC(this DateTime dt, string format)
        {
            if (dt == null)
            {
                return "";
            }
            return dt.ToString(format);
        }
        public static string ToCustomColor(this string valueobj, string value)
        {
            return string.Format(" <span style=\"color:" + value + "\">{0}</span>", valueobj);
        }


        private static string ColorBlue(string value)
        {
            return string.Format(" <span style=\"color:Blue\">{0}</span>", value);
        }

        private static string ColorRed(string value)
        {
            return string.Format(" <span style=\"color:Red\">{0}</span>", value);
        }
        private static string N0(string value)
        {
            double result = double.NaN;
            if (TryParseDouble(value, out result) == false)
            {
                return string.Empty;
            }
            if (double.IsNaN(result))
            {
                return "--";
            }

            return result.ToString("N0");
        }
        private static string N1(string value)
        {
            double result = double.NaN;
            if (TryParseDouble(value, out result) == false)
            {
                return string.Empty;
            }
            if (double.IsNaN(result))
            {
                return "--";
            }

            return result.ToString("N1");
        }
        private static string N2(string value)
        {
            double result = double.NaN;
            if (TryParseDouble(value, out result) == false)
            {
                return string.Empty;
            }
            if (double.IsNaN(result))
            {
                return "--";
            }

            return result.ToString("N2");
        }
        private static string P2(string value)
        {
            double result = double.NaN;
            if (TryParseDouble(value, out result) == false)
            {
                return string.Empty;
            }
            if (double.IsNaN(result))
            {
                return "-%";
            }

            return result.ToString("P2");
        }
        private static string Thous0(string value)
        {
            double result = double.NaN;
            if (TryParseDouble(value, out result) == false)
            {
                return string.Empty;
            }
            if (double.IsNaN(result))
            {
                return "-‰";
            }

            return string.Format("{0:N0}‰", (result * 1000));
        }
        private static string Thous1(string value)
        {
            double result = double.NaN;
            if (TryParseDouble(value, out result) == false)
            {
                return string.Empty;
            }
            if (double.IsNaN(result))
            {
                return "-‰";
            }

            return string.Format("{0:N1}‰", (result * 1000));
        }
        private static string Thous2(string value)
        {
            double result = double.NaN;
            if (TryParseDouble(value, out result) == false)
            {
                return string.Empty;
            }
            if (double.IsNaN(result))
            {
                return "-‰";
            }

            return string.Format("{0:N2}‰", (result * 1000));
        }
        private static string DateTime_HHmmss(string value)
        {
            if (string.IsNullOrEmpty(value) || value == "&nbsp;")
            {
                return string.Empty;
            }
            else
            {
                DateTime timeValue = DateTime.MinValue;
                if (DateTime.TryParse(value, out timeValue))
                {
                    return timeValue.ToString("HH:mm:ss");
                }
            }
            return value;
        }
        private static string DateTime_HHmmss1(string value)
        {
            if (string.IsNullOrEmpty(value) || value == "&nbsp;")
            {
                return string.Empty;
            }
            else
            {
                DateTime timeValue = DateTime.MinValue;
                if (DateTime.TryParse(value, out timeValue))
                {
                    return timeValue.ToString("HHmmss");
                }
            }
            return value;
        }
        private static string DateTime_yyyyMMdd(string value)
        {
            if (string.IsNullOrEmpty(value) || value == "&nbsp;")
            {
                return string.Empty;
            }
            else
            {
                DateTime timeValue = DateTime.MinValue;
                if (DateTime.TryParse(value, out timeValue))
                {
                    return timeValue.ToString("yyyy-MM-dd");
                }
            }
            return value;
        }
        private static string DateTime_yyyyMMdd1(string value)
        {
            if (string.IsNullOrEmpty(value) || value == "&nbsp;")
            {
                return string.Empty;
            }
            else
            {
                DateTime timeValue = DateTime.MinValue;
                if (DateTime.TryParse(value, out timeValue))
                {
                    return timeValue.ToString("yyyyMMdd");
                }
            }
            return value;
        }
        private static string DateTime_yyyyMMddHHmmss(string value)
        {
            if (string.IsNullOrEmpty(value) || value == "&nbsp;")
            {
                return string.Empty;
            }
            else
            {
                DateTime timeValue = DateTime.MinValue;
                if (DateTime.TryParse(value, out timeValue))
                {
                    return timeValue.ToString("yyyy-MM-dd HH:mm:ss");
                }
            }
            return value;
        }
        private static string DateTime_yyyyMMddHHmmss1(string value)
        {
            if (string.IsNullOrEmpty(value) || value == "&nbsp;")
            {
                return string.Empty;
            }
            else
            {
                DateTime timeValue = DateTime.MinValue;
                if (DateTime.TryParse(value, out timeValue))
                {
                    return timeValue.ToString("yyyyMMddHHmmss");
                }
            }
            return value;
        }
        /// <summary>
        /// Dates the time_yyyy M MDD H HMMSS nohen.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private static string DateTime_yyyyMMddHHmmssNohen(string value)
        {

            if (string.IsNullOrEmpty(value) || value == "&nbsp;")
            {
                return string.Empty;
            }
            else
            {
                DateTime timeValue = DateTime.MinValue;
                if (DateTime.TryParse(value, out timeValue))
                {
                    return timeValue.ToString("yyyyMMddHHmmss");
                }
            }
            return value;
        }
        private static string DateTime_yyyyMMddBRHHmmss(string value)
        {
            if (string.IsNullOrEmpty(value) || value == "&nbsp;")
            {
                return string.Empty;
            }
            else
            {
                DateTime timeValue = DateTime.MinValue;
                if (DateTime.TryParse(value, out timeValue))
                {
                    return timeValue.ToString("yyyy-MM-dd <br/> HH:mm:ss");
                }
            }
            return value;
        }

        private static bool TryParseDouble(string value, out double num)
        {
            if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(value.Trim()) || value == "&nbsp;")
            {
                num = double.NaN;
                return false;
            }

            value = value.TrimEnd("%".ToCharArray());

            if (double.TryParse(value.ToString().Trim(), NumberStyles.Any, CultureInfo.CurrentCulture, out num))
            {
                return true;
            }

            return false;

        }
        
        public static string NewsIsComment(this object intg)
        {
            int v = int.Parse(intg.ToString());
            List<string> li = new List<string>();
            var data = EnumHelper.GetEnumList(typeof(ENums.NewsIsComment), false, false);
            if (data != null)
            {
                foreach (var item in data)
                {
                    int vv = int.Parse(item.Value);
                    if (v == vv)
                    {
                        li.Add(item.Text);
                    }
                }
            }
            return string.Join(",", li.ToArray());
        }

        public static string Status(this object intg)
        {
            int v = int.Parse(intg.ToString());
            List<string> li = new List<string>();
            var data = EnumHelper.GetEnumList(typeof(ENums.EStatus), false, false);
            if (data != null)
            {
                foreach (var item in data)
                {
                    int vv = int.Parse(item.Value);
                    if (v == vv)
                    {
                        li.Add(item.Text);
                    }
                }
            }
            return string.Join(",", li.ToArray());
        }

        public static string NewCardStatus(this object intg)
        {
            int v = int.Parse(intg.ToString());
            List<string> li = new List<string>();
            var data = EnumHelper.GetEnumList(typeof(ENums.ENewCardStatus), false, false);
            if (data != null)
            {
                foreach (var item in data)
                {
                    int vv = int.Parse(item.Value);
                    if (v == vv)
                    {
                        li.Add(item.Text);
                    }
                }
            }
            return string.Join(",", li.ToArray());
        }

        public static string SwapEmail(this string email)
        {
            if (ValidationUtil.IsEmail(email) == false)
            {
                return email;
            }
            string[] arr = email.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries);
            string left = arr[0];
            string right = arr[1];
            if (left.Length == 1)
            {
                left = "*";
            }
            else
            {
                double t = Math.Round(left.Length / 3d);
                double r = left.Length - t;
                for (int i = 0; i < r; i++)
                {
                    left = left.Replace(left[i], '*');
                }
            }

            return left + "@" + right;
        }

        /// <summary>
        /// trim 和 tolower
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string TrimLower(this string str)
        {
            if (!string.IsNullOrWhiteSpace(str))
            {
                return str.Trim().ToLower();
            }
            return str;
        }
        public static string HtmlDecode(this string str)
        {
            return System.Web.HttpContext.Current.Server.HtmlDecode(str);
        }

        public static string HtmlEncode(this string str)
        {
            return System.Web.HttpContext.Current.Server.HtmlEncode(str);
        }
    }



}
